//
//  KDNewsCellModelFrame.m
//  YuQingXiaoFang
//
//  Created by wangdongdong on 18/6/11.
//  Copyright © 2018年 KDTech. All rights reserved.
//

#import "KDNewsCellModelFrame.h"
#import "KDNewsCellModel.h"
/** cell的边框宽度 */
#define KDNewsCellBorder 5

@implementation KDNewsCellModelFrame
-(void) setModel:(KDNewsCellModel *)model{
    _model=model;
    
    
    //1.图片
    CGFloat imageW=105;
    CGFloat imageH=imageW*2/3;
    CGFloat imageX=KDNewsCellBorder;
    CGFloat imageY=KDNewsCellBorder;
    if(model.newsType!=NEW_TYPE_INTERNEWS && model.newsType!=NEW_TYPE_ABROAD){
        imageW=0;
        imageH=0;
        imageX=0;
    }
    _leftViewF=CGRectMake(imageX, imageY, imageW, imageH);
    
    //标题
    CGFloat titleLabelX=CGRectGetMaxX(_leftViewF)+KDNewsCellBorder;
    CGFloat titleLabelY=imageY;
    CGSize titleLabelSize = [model.title sizeWithAttributes:@{NSFontAttributeName :KDNewsCellModelTitleFont}];
    CGFloat titleW=titleLabelSize.width;
    //标题超出屏幕宽度，则设置为屏幕宽度
    if(titleW>(screenW-KDNewsCellBorder-titleLabelX)){
        titleLabelSize.width=screenW-KDNewsCellBorder-titleLabelX;
    }
    _titleLabelF=(CGRect){{titleLabelX,titleLabelY},titleLabelSize};
    
    
    //时间
    CGFloat timeLabelX=titleLabelX;
    CGFloat timeLabelY=CGRectGetMaxY(_titleLabelF)+KDNewsCellBorder;
    NSString *pushTimes=[NSString stringWithFormat:@"%ld",model.pushTimes];
    CGSize timeLabelSize=[pushTimes sizeWithAttributes:@{NSFontAttributeName :KDNewsCellModelTimeFont}];
    timeLabelSize.width=110;
    _timeLabelF=(CGRect){{timeLabelX,timeLabelY},timeLabelSize};
    
    //来源或作者
    CGFloat sourceLabelX=CGRectGetMaxX(_timeLabelF)+KDNewsCellBorder;
    CGFloat sourceLabelY=timeLabelY;
    CGSize sourceLabelSize=[model.source sizeWithAttributes:@{NSFontAttributeName :KDNewsCellModelTimeFont}];
    _sourceLabelF=(CGRect){{sourceLabelX,sourceLabelY},sourceLabelSize};
    
    //内容
    CGFloat contentLabelX=0;
    CGFloat contentLabelY=CGRectGetMaxY(_timeLabelF);
    CGSize contentLabelSize=CGSizeMake(0, 0);
    if(model.newsType!=NEW_TYPE_INTERNEWS && model.newsType!=NEW_TYPE_ABROAD){
        contentLabelX=titleLabelX;
        contentLabelY=CGRectGetMaxY(_timeLabelF)+KDNewsCellBorder;
        CGFloat contentLabelMaxW=screenW-KDNewsCellBorder*2;
        
        //计算单行高度，为了处理bug（比如5行显示2行字，另外3行空白）
        CGFloat contentLabelH=[self getContentLabelSize:@"单行高度" AndContentLabelMaxW:contentLabelMaxW].height;
        //计算内容高度
        contentLabelSize = [self getContentLabelSize:model.summary AndContentLabelMaxW:contentLabelMaxW];
        
        //大于1行时，显示2行；否则显示1行
        if(contentLabelSize.height>contentLabelH){
            contentLabelSize.height=contentLabelH*2;
        }else{
            contentLabelSize.height=contentLabelH;
        }
    }
    _contentLabelF=(CGRect){{contentLabelX,contentLabelY},contentLabelSize};
    
    //关注
    CGSize focusButtonSize=[@"关注" sizeWithAttributes:@{NSFontAttributeName:KDNewsCellModelButtonFont}];
    CGFloat focusButtonX=screenW-KDNewsCellBorder-focusButtonSize.width;
    CGFloat focusButtonY;
    if(CGRectGetMaxY(_leftViewF)>CGRectGetMaxY(_contentLabelF)){
        focusButtonY= CGRectGetMaxY(_leftViewF)-focusButtonSize.height;
    }else{
        focusButtonY=CGRectGetMaxY(_contentLabelF)+KDNewsCellBorder;
    }
    _focusButtonF=(CGRect){{focusButtonX,focusButtonY},focusButtonSize};
    
    
    //查看批示
    CGFloat lookButtonX=0;
    if(model.commentStatus>=1){
        CGFloat lookButtonY=focusButtonY;
        CGSize lookButtonSize=[@"查看批示" sizeWithAttributes:@{NSFontAttributeName:KDNewsCellModelButtonFont}];
         lookButtonX=focusButtonX-KDNewsCellBorder-lookButtonSize.width;
        _lookButtonF=(CGRect){{lookButtonX,lookButtonY},lookButtonSize};
    }
    

    //预警级别
    CGFloat starViewW=_focusButtonF.size.height;
    CGFloat starViewH=_focusButtonF.size.height;
    CGFloat starViewY=focusButtonY;
    //低级
    if(model.newsLevel==0 || model.newsLevel==1 || model.newsLevel==2 || model.newsLevel==3){
        CGFloat star1ViewX;
        //是否领导批示 1为已批
        if(model.commentStatus>=1){
            star1ViewX=lookButtonX-KDNewsCellBorder-starViewW;
        }else{
            star1ViewX=focusButtonX-KDNewsCellBorder-starViewW;
        }
        _star1ViewF=CGRectMake(star1ViewX, starViewY, starViewW, starViewH);
        }
    //中级
    if(model.newsLevel==2 || model.newsLevel==3){
        CGFloat star2ViewX=_star1ViewF.origin.x-starViewW;
        _star2ViewF=CGRectMake(star2ViewX, starViewY, starViewW, starViewH);
    }
    //高级
    if(model.newsLevel==3){
        CGFloat star3ViewX=_star2ViewF.origin.x-starViewW;
        _star3ViewF=CGRectMake(star3ViewX, starViewY, starViewW, starViewH);
    }
    
       // 计算cell的高度
    _cellHeight = MAX(CGRectGetMaxY(_leftViewF), CGRectGetMaxY(_focusButtonF))+KDNewsCellBorder;
    
    
    
    
}
//计算内容size
-(CGSize) getContentLabelSize:(NSString*)contentStr AndContentLabelMaxW:(CGFloat)contentLabelMaxW{
    CGSize contentLabelSize= [contentStr boundingRectWithSize:CGSizeMake(contentLabelMaxW, MAXFLOAT)
                                                          options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin
                                                       attributes:@{NSFontAttributeName :KDNewsCellModelContentFont} context:nil].size;
    return contentLabelSize;
}
@end
